using UnityEngine;
using UnityEngine.UI;

public class UIController : MonoBehaviour
{
    [SerializeField] private GameObject uiToHide; // 要隐藏的UI对象
    [SerializeField] private GameObject uiToShow; // 要显示的UI对象
    private bool isUIShown = false; // 跟踪UI是否显示 1开真 0 关假

    // 在Inspector面板中设置UI对象
    public void SetUIReferences(GameObject hideUI, GameObject showUI)
    {
        uiToHide = hideUI;
        uiToShow = showUI;
    }

    // 隐藏一个UI并显示另一个UI
    public void SwitchUI()
    {
        if (uiToHide != null) //null 空 none
        {
            uiToHide.SetActive(false); // 隐藏UI
        }
        else
        {
            Debug.LogWarning("要隐藏的UI对象未设置！");
        }

        if (uiToShow != null)
        {
            uiToShow.SetActive(true); // 显示UI true真 1 False假 0
            isUIShown = true;
        }
        else
        {
            Debug.LogWarning("要显示的UI对象未设置！");
        }
    }

    // 单独设置一个UI的显示状态
    public void SetUIActive(GameObject ui, bool isActive)
    {
        if (ui != null)
        {
            ui.SetActive(isActive);
            if (ui == uiToShow)
            {
                isUIShown = isActive;
            }
        }
        else
        {
            Debug.LogWarning("UI对象未设置！");
        }
    }

    private void Update()
    {
        // 检测按M键
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (uiToShow != null)
            {
                isUIShown = !isUIShown;
                uiToShow.SetActive(isUIShown);
            }
            else
            {
                Debug.LogWarning("要显示的UI对象未设置！");
            }
        }
    }

    private void Start()
    {
        // 确保UI对象已设置
        if (uiToHide == null || uiToShow == null) 
        {
            Debug.LogWarning("UI对象未设置！");
        }
    }
}